package com.lixh.virtual.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.lixh.virtual.R;


public class PageIndexView extends View {

    protected int pageCnt;

    protected int pageIndex;

    protected int pageIndexMarginWidth;

    protected int pageIndexWidth;

    protected final Paint pageIndexColor;

    protected final Paint curPageIndexColor;

    public PageIndexView(Context context) {
        this(context, null);
    }

    public PageIndexView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PageIndexView(Context context, AttributeSet attributeSet, int defStyleAttr) {
        super(context, attributeSet, defStyleAttr);
        this.pageIndexColor = new Paint();
        this.curPageIndexColor = new Paint();
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.PageIndexView, defStyleAttr, 0);
        this.pageCnt = obtainStyledAttributes.getInt(0, 0);
        this.pageIndexColor.setColor(obtainStyledAttributes.getColor(R.styleable.PageIndexView_page_index_color, 0));
        this.pageIndexColor.setAntiAlias(true);
        this.curPageIndexColor.setColor(obtainStyledAttributes.getColor(R.styleable.PageIndexView_cur_page_index_color, -1));
        this.curPageIndexColor.setAntiAlias(true);
        this.pageIndexMarginWidth = (int) obtainStyledAttributes.getDimension(R.styleable.PageIndexView_page_index_margin_width, 0.0f);
        this.pageIndexWidth = (int) obtainStyledAttributes.getDimension(R.styleable.PageIndexView_page_index_width, 0.0f);
        obtainStyledAttributes.recycle();
    }

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int i3;
        if (this.pageCnt > 1) {
            i3 = (this.pageCnt * this.pageIndexMarginWidth) + ((this.pageCnt - 1) * this.pageIndexWidth);
        } else {
            i3 = 0;
        }
        setMeasuredDimension(mo5059a(i3, widthMeasureSpec), mo5059a(0, heightMeasureSpec));
    }

    public int mo5059a(int i, int i2) {
        int mode = View.MeasureSpec.getMode(i2);
        int size = View.MeasureSpec.getSize(i2);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                return Math.min(i, size);
            case MeasureSpec.UNSPECIFIED:
                return i;
            case MeasureSpec.EXACTLY:
                return size;
            default:
                return 0;
        }
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.pageCnt > 1) {
            int i = 0;
            while (i < this.pageCnt) {
                canvas.drawCircle((float) (((this.pageIndexMarginWidth + this.pageIndexWidth) * i) + (this.pageIndexMarginWidth / 2)), (float) (this.pageIndexMarginWidth / 2), (float) (this.pageIndexMarginWidth / 2), i == this.pageIndex ? this.curPageIndexColor : this.pageIndexColor);
                i++;
            }
        }
    }

    public void setPageCnt(int pageCnt) {
        if (pageCnt != this.pageCnt) {
            this.pageCnt = pageCnt;
            requestLayout();
            invalidate();
        }
    }

    public int getPageCnt() {
        return this.pageCnt;
    }

    public void setCurPageIndex(int pageIndex) {
        if (this.pageIndex != pageIndex) {
            this.pageIndex = pageIndex;
            invalidate();
        }
    }

    public int getCurPageIndex() {
        return this.pageIndex;
    }
}
